package bank.model.dao.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bank.model.pool.*;
import bank.model.dao.EmployeeDAO;
import bank.model.sql.builder.SqlString;
import bank.model.properties.*;
import bank.model.vo.*;

public class MySqlEmployeeDAO implements EmployeeDAO ,DBEmployeeTable, DBTables{
	public List getEmployeeInDepartment(int id){
		ResultSet res;
		List<Employee> empl = new ArrayList<Employee>();
		SqlString sql = new SqlString();
		sql.set_Select();
		sql.set_Table_Name(TBL_EMPLOYEES);
		sql.set_all();
		sql.add_Cond(EMPLOYEE_COL_DEPARTMENT_ID, Integer.toString(id));
		System.out.println(sql.generate());
		System.out.println(sql.get_Command());
		res = sql.executeQuery();
		try {
			while(res.next())
			{
				Employee employee = new Employee(res.getInt(EMPLOYEE_COL_ID),
						res.getString(EMPLOYEE_COL_FIRSTNAME),res.getString(EMPLOYEE_COL_LASTNAME),
						res.getString(EMPLOYEE_COL_LASTNAME),res.getInt(EMPLOYEE_COL_DEPARTMENT_ID),res.getInt(EMPLOYEE_COL_LOGIN_ID));
				empl.add(employee);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return empl;
	}
	
	public void deleteEmployee(int id)
	{
		SqlString sql = new SqlString();
		sql.set_Delete();
		sql.set_Table_Name(TBL_EMPLOYEES);
		sql.add_Cond(EMPLOYEE_COL_ID, Integer.toString(id));
		System.out.println(sql.generate());
		System.out.println(sql.get_Command());
		sql.executeUpdate();
		
		
	}
	
	public void addEmployee(Employee employee)
	{
		SqlString sql = new SqlString();
		sql.set_Insert();
		sql.set_Table_Name(TBL_EMPLOYEES);
		sql.add_Param(EMPLOYEE_COL_FIRSTNAME, employee.getFirstName());
		sql.add_Param(EMPLOYEE_COL_LASTNAME, employee.getLastName());
		sql.add_Param(EMPLOYEE_COL_SUBNAME, employee.getSubName());
		sql.add_Param(EMPLOYEE_COL_DEPARTMENT_ID, Integer.toString(employee.getDepartment()));
		sql.add_Param(EMPLOYEE_COL_LOGIN_ID, Integer.toString(employee.getLogin()));
		System.out.println(sql.generate());
		System.out.println(sql.get_Command());
		sql.executeUpdate();
	}
	
	public void editEmployee(int id, Employee employee)
	{
		SqlString sql = new SqlString();
		sql.set_Update();
		sql.set_Table_Name(TBL_EMPLOYEES);
		sql.add_Param(EMPLOYEE_COL_FIRSTNAME, employee.getFirstName());
		sql.add_Param(EMPLOYEE_COL_LASTNAME, employee.getLastName());
		sql.add_Param(EMPLOYEE_COL_SUBNAME, employee.getSubName());
		sql.add_Param(EMPLOYEE_COL_DEPARTMENT_ID, Integer.toString(employee.getDepartment()));
		sql.add_Param(EMPLOYEE_COL_LOGIN_ID, Integer.toString(employee.getLogin()));
		sql.add_Cond(EMPLOYEE_COL_ID, Integer.toString(id));
		System.out.println(sql.generate());
		System.out.println(sql.get_Command());
		sql.executeUpdate();
	}
	
	public Employee findById(int id) throws Exception
	{
		ResultSet res;
		SqlString sql = new SqlString();
		sql.set_Select();
		sql.set_Table_Name(TBL_EMPLOYEES);
		sql.set_all();
		sql.add_Cond(EMPLOYEE_COL_ID, Integer.toString(id));
		System.out.println(sql.generate());
		System.out.println(sql.get_Command());
		res = sql.executeQuery();
		res.first();
		Employee employee = new Employee(res.getInt(EMPLOYEE_COL_ID),
				res.getString(EMPLOYEE_COL_FIRSTNAME),res.getString(EMPLOYEE_COL_LASTNAME),
				res.getString(EMPLOYEE_COL_LASTNAME),res.getInt(EMPLOYEE_COL_DEPARTMENT_ID),res.getInt(EMPLOYEE_COL_LOGIN_ID));
		return employee;

	}

}
